﻿Imports System.Data.OleDb

Public Class Details
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Only teachers can see answers to questions
        If Roles.IsUserInRole("teacher") Then
            GridView1.Columns(1).Visible = True
        Else
            GridView1.Columns(1).Visible = False
        End If

        'Checks if there is a questionID in the querystring, otherwise redirects to default page
        If IsPostBack = False Then
            If Request.Params("questionID") = "" Then Response.Redirect("Default.aspx")
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM comment WHERE questionID=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
            oleDbConn.Open()
            Dim dataReader = cmd.ExecuteReader()
            GridView2.DataSource = dataReader
            GridView2.DataBind()

            'This is used to see if a user has looked at a question before. This can be used for statistical purposes
            Dim questionsViewedSql As String = "INSERT INTO questionsViewed (author, questionID) VALUES (@f1,@f2)"
            Dim cmd2 As OleDbCommand = New OleDbCommand(questionsViewedSql, oleDbConn)
            cmd2.CommandType = CommandType.Text
            cmd2.Parameters.AddWithValue("@f1", User.Identity.Name)
            cmd2.Parameters.AddWithValue("@f2", Request.Params("questionID"))
            cmd2.ExecuteNonQuery()
        End If

        'Checks if the user has already liked a question or not
        If IsPostBack = False Then
            Dim oleDbConn3 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
            oleDbConn3.Open()
            Dim doesLikeOrNotSql As String = "SELECT * FROM likes WHERE questionID=@f1 AND author=@f2"
            Dim doesLikeOrNotCmd As New OleDbCommand(doesLikeOrNotSql, oleDbConn3)
            doesLikeOrNotCmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
            doesLikeOrNotCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim dr2 As OleDbDataReader = doesLikeOrNotCmd.ExecuteReader()
            If (dr2.HasRows()) Then
                btn_like.Visible = False
                lbl_like.Visible = True

            Else
                btn_like.Visible = True
                lbl_like.Visible = False

            End If
        End If

        'Check if a user has already rated a question
        If IsPostBack = False Then
            Dim oleDbConn10 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
            oleDbConn10.Open()
            Dim doesRateOrNotSql As String = "SELECT * FROM ratings WHERE questionID=@f1 AND author=@f2"
            Dim doesRateOrNotCmd As New OleDbCommand(doesRateOrNotSql, oleDbConn10)
            doesRateOrNotCmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
            doesRateOrNotCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
            Dim dr10 As OleDbDataReader = doesRateOrNotCmd.ExecuteReader()
            If (dr10.HasRows()) Then
                btn_rating.Visible = False
                Rating1.Visible = False
                lbl_rate.Visible = False
                Label1.Visible = True

            Else
                btn_rating.Visible = True
                Rating1.Visible = True
                lbl_rate.Visible = True
                Label1.Visible = False

            End If
        End If

    End Sub

    Protected Sub btn_submit_comment_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_submit_comment.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into comment(questionID, author, commentBody, commentTitle) Values (@f1,@f2,@f3,@f4)"
        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text

        cmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
        cmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        cmd.Parameters.AddWithValue("@f3", tb_commentBody.Text)
        cmd.Parameters.AddWithValue("@f4", tb_commentTitle.Text)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        Dim fillComments As String = "SELECT * FROM comment WHERE questionID=@f1"
        Dim fillCommentsCmd As OleDbCommand = New OleDbCommand(fillComments, oleDbConn)
        fillCommentsCmd.CommandType = CommandType.Text
        fillCommentsCmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
        Dim dataReader = fillCommentsCmd.ExecuteReader()
        GridView2.DataSource = dataReader
        GridView2.DataBind()

        tb_commentBody.Text = ""
        tb_commentTitle.Text = ""

    End Sub

    Protected Sub btn_addTag_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_addTag.Click
        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
        Dim SqlString As String = "Insert into questionTagJoin(questionID, tagID) Values (@f1,@f2)"
        oleDbConn.Open()
        For Each cb As ListItem In cb_tags.Items
            If cb.Selected Then
                'here is where we add the join to the table of tags and questions
                Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
                cmd.Parameters.AddWithValue("@f2", cb.Value)
                cmd.ExecuteNonQuery()
            End If
        Next

        Dim newTags = tb_addNewTags.Text.Split(",")
        For Each newTag In newTags
            'first, add the new tag to the tag table
            Dim newTagSql As String = "INSERT INTO tag(tagBody) VALUES(@f1)"
            Dim cmd As OleDbCommand = New OleDbCommand(newTagSql, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", newTag)
            cmd.ExecuteNonQuery()

            'then, find the ID of the new tag
            Dim getLastIDCmd As OleDbCommand = New OleDbCommand("SELECT TOP 1 tagID from tag ORDER BY tagID DESC", oleDbConn)
            Dim dr = getLastIDCmd.ExecuteReader()
            dr.Read()
            Dim lastID = dr.GetValue(0)

            'associate tagID with questionID
            Dim newcmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            newcmd.CommandType = CommandType.Text
            newcmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
            newcmd.Parameters.AddWithValue("@f2", lastID)
            newcmd.ExecuteNonQuery()

        Next

    End Sub

    Protected Sub btn_rating_click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_rating.Click
        Dim oleDbConn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim delRatingSql As String = "DELETE FROM ratings WHERE questionID=@f1 AND author=@f3"
        Dim delRatingCmd As New OleDbCommand(delRatingSql, oleDbConn)
        delRatingCmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
        delRatingCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        delRatingCmd.ExecuteNonQuery()

        Dim addRatingSql As String = "INSERT INTO ratings (questionID, ratingValue, author) VALUES (@f1, @f2, @f3)"
        Dim addRatingCmd As New OleDbCommand(addRatingSql, oleDbConn)
        addRatingCmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
        addRatingCmd.Parameters.AddWithValue("@f2", Rating1.CurrentRating)
        addRatingCmd.Parameters.AddWithValue("@f3", User.Identity.Name)
        addRatingCmd.ExecuteNonQuery()

        Label1.Text = "Thank you for adding your rating"

    End Sub

    Protected Sub btn_like_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_like.Click
        Dim oleDbConn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ExamQuestionsConnectionString").ConnectionString)
        oleDbConn.Open()
        Dim addLikeSql As String = "INSERT INTO likes (questionID, author) VALUES (@f1, @f2)"
        Dim addLikeCmd As New OleDbCommand(addLikeSql, oleDbConn)
        addLikeCmd.Parameters.AddWithValue("@f1", Request.Params("questionID"))
        addLikeCmd.Parameters.AddWithValue("@f2", User.Identity.Name)
        addLikeCmd.ExecuteNonQuery()

        btn_like.Visible = False
        lbl_like.Visible = True

    End Sub
End Class